package boundarys;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import controllers.AddressCustomerController;
import entitys.Customer;
import entitys.Reply;
import enums.Command;

public class AddressCustomerGUI extends AbstractGUI{
	/**
	 * AddressClient frame
	 */
	private JFrame frmAddressClient;
	/**
	 * First name text field
	 */
	private JTextField FirstnameTextField;
	/**
	 * last name text field
	 */
	private JTextField LastnameTextField;
	/**
	 * Address text field
	 */
	private JTextField AddressTextField;
	/**
	 * phone text field
	 */
	private JTextField PhoneTextField;
	/**
	 * Email text field
	 */
	private JTextField EmailTextField;
	/**
	 * ID text field
	 */
	private JTextField IDTextField;
	/**
	 * List of products combo box
	 */
	JComboBox ListofCustComboBox;
	/**
	 * Customers List
	 */
	ArrayList<Customer> res;
	/**
	 * List of marketing sales.
	 */
	JComboBox marketingSalecomboBox=null;
	/**
	 * Construct Address Customer GUI
	 */
	public AddressCustomerGUI() {
		initialize();
		initGUI(frmAddressClient);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		String s;
		frmAddressClient = new JFrame();
		frmAddressClient.setTitle("... Manage Clients > Address Client");
		frmAddressClient.setBounds(100, 100, 350, 374);
		frmAddressClient.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frmAddressClient.getContentPane().setLayout(null);
		
		FirstnameTextField = new JTextField();
		FirstnameTextField.setEditable(false);
		FirstnameTextField.setBounds(108, 101, 216, 20);
		frmAddressClient.getContentPane().add(FirstnameTextField);
		FirstnameTextField.setColumns(10);
		
		LastnameTextField = new JTextField();
		LastnameTextField.setEditable(false);
		LastnameTextField.setBounds(108, 132, 216, 20);
		frmAddressClient.getContentPane().add(LastnameTextField);
		LastnameTextField.setColumns(10);
		
		AddressTextField = new JTextField();
		AddressTextField.setEditable(false);
		AddressTextField.setBounds(108, 222, 216, 20);
		frmAddressClient.getContentPane().add(AddressTextField);
		AddressTextField.setColumns(10);
		
		PhoneTextField = new JTextField();
		PhoneTextField.setEditable(false);
		PhoneTextField.setBounds(108, 163, 216, 20);
		frmAddressClient.getContentPane().add(PhoneTextField);
		PhoneTextField.setColumns(10);
		
		EmailTextField = new JTextField();
		EmailTextField.setEditable(false);
		EmailTextField.setBounds(108, 191, 216, 20);
		frmAddressClient.getContentPane().add(EmailTextField);
		EmailTextField.setColumns(10);
		
		IDTextField = new JTextField();
		IDTextField.setEditable(false);
		IDTextField.setBounds(108, 70, 216, 20);
		frmAddressClient.getContentPane().add(IDTextField);
		IDTextField.setColumns(10);
		IDTextField.disable();
		
		JLabel lblFirstName = new JLabel("First Name :");
		lblFirstName.setBounds(29, 104, 94, 14);
		frmAddressClient.getContentPane().add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name :");
		lblLastName.setBounds(29, 135, 94, 14);
		frmAddressClient.getContentPane().add(lblLastName);
		
		JLabel lblAddress = new JLabel("Address :");
		lblAddress.setBounds(29, 228, 94, 14);
		frmAddressClient.getContentPane().add(lblAddress);
		
		JLabel lblPhone = new JLabel("Phone :");
		lblPhone.setBounds(29, 166, 94, 14);
		frmAddressClient.getContentPane().add(lblPhone);
		
		JLabel lblEmail = new JLabel("Email :");
		lblEmail.setBounds(29, 197, 94, 14);
		frmAddressClient.getContentPane().add(lblEmail);
		
		JLabel lblId = new JLabel("ID :");
		lblId.setBounds(29, 73, 46, 14);
		frmAddressClient.getContentPane().add(lblId);
		
		JButton btnConfirm = new JButton("Confirm");
	
		btnConfirm.setBounds(211, 278, 89, 23);
		frmAddressClient.getContentPane().add(btnConfirm);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				goBack();
			}
		});
		btnCancel.setBounds(29, 278, 89, 23);
		frmAddressClient.getContentPane().add(btnCancel);
		
		ListofCustComboBox = new JComboBox();
		ListofCustComboBox.setEnabled(false);

		ListofCustComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Customer c = (Customer) ListofCustComboBox.getSelectedItem();
				if(c==null)
				{
					IDTextField.setText("");
					FirstnameTextField.setText("");
					LastnameTextField.setText("");
					PhoneTextField.setText("");
					EmailTextField.setText("");
					AddressTextField.setText("");
				}
				else
				{
					IDTextField.setText(c.getId());
					FirstnameTextField.setText(c.getFirstName());
					LastnameTextField.setText(c.getLastName());
					PhoneTextField.setText(c.getPhone());
					EmailTextField.setText(c.getEmail());
					AddressTextField.setText(c.getAddress());
				}
				//chckbxAllow.setSelected(Integer.parseInt(splited[6])==1);
			}
		});
		ListofCustComboBox.setBounds(29, 39, 197, 20);
		frmAddressClient.getContentPane().add(ListofCustComboBox);
		
		
		
		JLabel lblChooseCustomer = new JLabel("");
		lblChooseCustomer.setBounds(29, 28, 106, 14);
		frmAddressClient.getContentPane().add(lblChooseCustomer);
		
		JCheckBox chckbxReffer = new JCheckBox("Reffer Later?");
		chckbxReffer.setSelected(true);

		chckbxReffer.setBounds(29, 249, 225, 23);
		
		btnConfirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddressCustomerController.addressClient(IDTextField.getText(),chckbxReffer.isSelected());
				new AddReportGUI(Integer.parseInt(IDTextField.getText()),marketingSalecomboBox.getSelectedItem().toString());
				frame.setVisible(false);
			}
		});
		frmAddressClient.getContentPane().add(chckbxReffer);
		
		JButton refreshBtn = new JButton("Refresh");

		refreshBtn.setBounds(231, 11, 93, 48);
		frmAddressClient.getContentPane().add(refreshBtn);
		
		marketingSalecomboBox = new JComboBox();
		marketingSalecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(marketingSalecomboBox.getSelectedItem()!=null)
					AddressCustomerController.getClients(marketingSalecomboBox.getSelectedItem().toString());
				else
					ListofCustComboBox.removeAllItems();
			}
		});
		marketingSalecomboBox.setBounds(29, 11, 197, 20);
		frmAddressClient.getContentPane().add(marketingSalecomboBox);
		
		refreshBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddressCustomerController.getMarketingSales();
			}
		});
		AddressCustomerController.getMarketingSales();
		frmAddressClient.setVisible(true);
		
	}
	
	
	@Override
	/**
	 * AbstractGUI's overridden method for getting replies from server 
	 */
	public void getReply(Object r) {
		
		
		if(((Reply)r).command.equals(Command.GET_CLIENTS_WITH_PERMIT))
		{
			res=(ArrayList<Customer>) ((Reply)r).result;
			ListofCustComboBox.removeAllItems();

				for(int i=0;i<res.size();i++)
				{
						ListofCustComboBox.addItem(res.get(i));
				}
			
			ListofCustComboBox.repaint();
		}
		else if(((Reply)r).command.equals(Command.GET_MARKETING_SALES_BY_ID))
		{
			
			ArrayList<String> msales =(ArrayList<String>) ((Reply)r).result;
			marketingSalecomboBox.removeAllItems();
			for(int i=0;i<msales.size();i++)
			{
				
				marketingSalecomboBox.addItem(msales.get(i));
			}
			ListofCustComboBox.setEnabled(true);
			ListofCustComboBox.repaint();
			
			
		}
		else if(((Reply)r).command.equals(Command.CHANGE_REFFER_PERMISSION))
		{
			
			System.out.println(((Reply)r).result);
			
			
		}
		else if(((Reply)r).command.equals(Command.SET_LAST_ADDRESSED))
		{
			
			System.out.println(((Reply)r).result);
			
			
		}
		
	}
}
